package cn.szabadsag.base.service.impl;

import cn.szabadsag.base.mapper.BaseMapper;
import cn.szabadsag.base.query.BaseQuery;
import cn.szabadsag.base.service.IBaseService;
import cn.szabadsag.utils.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 业务层公共实现类
 * @author yangcong
 * @date 2022年03月05日 22:09
 * @since v3.0
 */
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    @Transactional
    public void save(T t) {
        baseMapper.save(t);
    }

    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        baseMapper.delete(id);
    }

    @Override
    public T loadById(Long id) {
        return (T)baseMapper.loadById(id);
    }

    @Override
    public List<T> loadAll() {
        return baseMapper.loadAll();
    }

    /**
     * 高级查询+分页
     * @param query
     * @return
     */
    @Override
    public QueryResult<T> queryPage(BaseQuery query) {
        //  获取分页总条数
        Long count = baseMapper.queryCount(query);
        //  获取分页数据
        List<T> data = baseMapper.queryData(query);
        return new QueryResult<>(count,data);
    }
}
